﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Lavka.Core;
using Lavka.Mailers;
using Lavka.Models;
using Mvc.Mailer;

namespace Lavka.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class AdminController : Controller
    {
        private LavkaDbEntities context = new LavkaDbEntities();
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Patch()
        {
            string result = "";
            foreach (var product in context.Products)
            {
                try
                {
                    FileHelper.CreateThumbnail(product.Photo);
                    result += "<p>File <b>" + product.Photo + "</b> updated. </p>";
                }
                catch(Exception ex)
                {
                    result += ex.Message;
                }
            }
            ViewBag.Result = result;
            return View();
        }

        public ActionResult Statistics()
        {
            //MailSender.SendMail("ladeangel@mail.ru", CartHelper.Cart);
            return View("Statistics");
        }

        public ActionResult EmailSettings()
        {
            //MailSender.SendMail("ladeangel@mail.ru", CartHelper.Cart);
            return View(SettingsManager.SmtpSettings);           
        }

        [HttpPost]
        public ActionResult EmailSettings(SmtpSettings settings)
        {

            if(SettingsManager.SaveSmtpSettings(context, settings))
            {
                ViewBag.Message = "Настройки подключения к SMTP серверу сохранены!";
            }
            else ViewBag.Message = "Ошибка сохранения SMTP настроек!";

            return View(SettingsManager.SmtpSettings);
        }
    }
}
